package com.squareshoot.picCheckin;

import java.util.ArrayList;
import java.util.Locale;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class VenuesListAdapter extends ArrayAdapter<String> {
	int layout;
	Context context;
	ArrayList<String> venuesList;
	PhotosDownloader photosDl;
	//	HashMap<String,Bitmap> icons;
	ViewHolder holder;
	JSONObject jVenue;
	String language;

	static class ViewHolder{
		TextView groupText;
		ImageView catIcon;
		TextView venueName;
		TextView venueAddress;
	}

	public VenuesListAdapter(Context context, ArrayList<String> objects) {
		super(context, R.layout.venuesadapter, objects);

		this.layout = R.layout.venuesadapter;
		this.context = context;
		this.venuesList = objects;
		language = Locale.getDefault().getLanguage();
		if(Common.HIGHDEBUG){
			Log.e(Common.TAG,"language "+language);
		}

		photosDl = new PhotosDownloader(true,false,getContext().getResources().getDrawable(R.drawable.question));
	}

	public View getView(int position,View convertView,ViewGroup parent){

		if(convertView==null){	//Seulement la premiere fois qu'il cree la vue
			LayoutInflater inflater = LayoutInflater.from(context);
			convertView=inflater.inflate(layout, parent, false);

			holder = new ViewHolder();
			holder.groupText = (TextView) convertView.findViewById(R.id.venueAdapterGroup);
			holder.catIcon = (ImageView) convertView.findViewById(R.id.venueAdapterImage);
			holder.venueName = (TextView) convertView.findViewById(R.id.venueAdapterName);
			holder.venueAddress = (TextView) convertView.findViewById(R.id.venueAdapterAddress);

			convertView.setTag(holder);
		}else{
			holder = (ViewHolder)convertView.getTag();
		}

		try {
			jVenue = new JSONObject(venuesList.get(position));
			if(jVenue.has("jVenueGroup")){
				try{
					String group = jVenue.getString("jVenueGroup");
					holder.groupText.setVisibility(View.VISIBLE);
					holder.groupText.setText(getGroupName(group));
				}catch (JSONException e) {
					e.printStackTrace();
				}
			}else{
				holder.groupText.setVisibility(View.GONE);
			}

			String url;
			try{
				url=jVenue.getJSONObject("primarycategory").getString("iconurl");
			}catch(JSONException e){
				url=Common.defaultCatUrl;
			}
			photosDl.getPhoto(url,holder.catIcon);

			try{
				holder.venueName.setText(jVenue.getString("name"));
			}catch (JSONException e) {
				if(Common.HIGHDEBUG)
					Log.d(Common.TAG,"Venue has no name");
			}

			try{
				holder.venueAddress.setText(jVenue.getString("address"));
			}catch (JSONException e) {
				if(Common.HIGHDEBUG)
					Log.d(Common.TAG,"venue has no address");
			}
		}catch(JSONException e){
			if(Common.HIGHDEBUG)
				Log.d(Common.TAG,"error JSON in getView()");
		}

		return convertView;
	}

	private String getGroupName(String group){
		if(group.equals("Nearby")){
			return context.getString(R.string.nearby);
		}
		if(group.equals("My Favorites")){
			return context.getString(R.string.myfavorites);
		}
		if(group.equals("Trending Now")){
			return context.getString(R.string.trendingnow);
		}
		if(group.equals("Matching Places")){
			return context.getString(R.string.matchingplaces);
		}
		if(group.equals("Matching Tags")){
			return context.getString(R.string.matchingtags);
		}
		return group;
	}
}
